﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.WeixinService.Data;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class BigWheelStatList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;
        protected WxGamesConfig _gameDb;
        protected int _winCount, _useCount, _playCount, _prizeCount, _manCount;
        private int _uid;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            BindList(_framePage.PageIndex, _framePage.PageSize);
        }

        private void InitializeGlobalValue()
        {
            if (!IsPostBack)
            {
                ViewState["uid"] = Request["uid"];
            }

            if (!int.TryParse((string)ViewState["uid"], out _uid))
            {
            }
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxGamesAward>();
                SetupCondition(ref criteria);
                criteria.AddOrder(Order.Asc("Id")).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();

                _gameDb = session.CreateCriteria<WxGamesConfig>().Add(Expression.Eq("Id", _uid)).Add(Expression.Eq("GameType", "bigwheel")).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxGamesConfig>();

                IList<GameAwardItem> awardItemList = JsonConvert.DeserializeObject<IList<GameAwardItem>>(_gameDb.AwardConfig);
                _prizeCount = 0;
                foreach (GameAwardItem item in awardItemList)
                {
                    _prizeCount += item.Counter;
                }

                object obj = session.CreateCriteria<WxGamesAward>().Add(Expression.Eq("GameId", _uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Not(Expression.Eq("AwardStatus", "0"))).SetProjection(Projections.RowCount()).UniqueResult();
                _winCount = int.Parse(obj.ToString());

                obj = session.CreateCriteria<WxGamesAward>().Add(Expression.Eq("GameId", _uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Eq("AwardStatus", "9")).SetProjection(Projections.RowCount()).UniqueResult();
                _useCount = int.Parse(obj.ToString());

                _playCount = Convert.ToInt32(session.CreateSQLQuery("select count(*) from wx_games_log where game_id=" + _uid)

                    .UniqueResult());

                _manCount = Convert.ToInt32(session.CreateSQLQuery("select count(*) from (select wx_id from wx_games_log where game_id=" + _uid + " group by wx_id) as join_user_view")

                    .UniqueResult());
            }
        }

        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            criteria.Add(Expression.Eq("OwnerAccount", _userAccount));
            criteria.Add(Expression.Eq("GameId", _uid));
            criteria.Add(Expression.Eq("AwardStatus", "1"));
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxGamesAward>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }
    }
}
